<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="topbar::static">
</head>

<body>
<!--引用公用的导航栏-->
<nav th:replace="topbar::topbar"></nav>
<div class="container-fluid">
    <div class="row">
        <div th:replace="topbar::sidebar(activeUri='plugins.html')"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">5GC MANO</h1>
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#createPlugin">
                新增插件
            </button>
            <!--<h2 class="sub-header">Section title</h2>-->
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>description</th>
                        <th>workingTime</th>
                        <th>url</th>
                        <th>requiredService</th>
                        <th>installed</th>
                        <th>effective</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="plugin:${plugins}">
                        <td th:text="${plugin.id}"></td>
                        <td th:text="${plugin.name}"></td>
                        <td th:text="${plugin.description}"></td>
                        <td th:text="${plugin.workingTime}"></td>
                        <td th:text="${plugin.url}"></td>
                        <td th:text="${plugin.requiredService}"></td>
                        <td th:text="${plugin.installed}"></td>
                        <td th:text="${plugin.effective}"></td>
                        <td>
                            <button type="button" class="btn btn-danger" aria-label="Left Align"
                                    th:onclick="|deletePlugin(${plugin.id})|">
                                <span class="glyphicon glyphicon-remove"></span>
                            </button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!--模态框，用于创建插件-->
<div id="createPlugin" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
     aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新建插件</h4>
            </div>
            <div class="modal-body">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>description</th>
                        <th>workingTime</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td><input type="text" class="form-group" id="id"></td>
                        <td><input type="text" class="form-group" id="name"></td>
                        <td><input type="text" class="form-group" id="description"></td>
                        <td>
                            <select class="selectpicker" id="workingTime">
                                <option class="form-control" value="instantiate">instantiate</option>
                                <option class="form-control" value="runtime">runtime</option>
                            </select>
                        </td>
                    </tr>
                    </tbody>
                    <thead>
                    <tr>
                        <th>url</th>
                        <th>requiredService</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td><input type="text" class="form-group" id="url"></td>
                        <td>
                            <select class="selectpicker" id="requiredService" multiple>
                                <option class="form-control" value="imageManagement">imageManagement</option>
                                <option class="form-control" value="monitor">monitor</option>
                                <option class="form-control" value="NSDManagement">NSDManagement</option>
                                <option class="form-control" value="NSDTransform">NSDTransform</option>
                                <option class="form-control" value="NSOrchestrator">NSOrchestrator</option>
                                <option class="form-control" value="templateManagement">templateManagement</option>
                            </select>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="create()">保存</button>
            </div>
        </div>
    </div>
</div>

<script th:inline="javascript">
    var baseUrl = "http://"+hostIp+":8007/plugins/";

    function create() {
        //异步更新模板
        var url = baseUrl + "create";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "id": $("#id").val(),
                "templateName": $("#templateName").val(),
                "name": $("#name").val(),
                "description": $("#description").val(),
                "workingTime": $("#workingTime").val(),
                "url": $("#url").val(),
                "requiredService": $("#requiredService").val()
            },
            complete: function (id) {
                alert("插件保存成功");
                reload();
            }
        });
    }

    function deletePlugin(id) {
        var url = baseUrl + "delete";
        $.ajax({
            url: url,
            type: "POST",
            data: {
                "id": id
            },
            complete: function (id) {
                alert("插件删除成功");
                reload();
            }
        });
    }

    function reload() {
        $("#createPlugin").modal('hide');
        window.location.reload();
    }
</script>
</html>
